package cn.hp.service.edu.service.impl;

import cn.hp.service.edu.entity.Teacher;
import cn.hp.service.edu.entity.vo.TeacherQuery;
import cn.hp.service.edu.mapper.TeacherMapper;
import cn.hp.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author qf
 * @since 2022-03-17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public IPage<Teacher> pageQuery(Page<Teacher> teacherPage, TeacherQuery teacherQuery) {
        // 1.如果没有查询条件为普通分页
        if (teacherQuery == null) {
            return baseMapper.selectPage(teacherPage, null);
        }

        // 2.获取每个查询条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        // 3.条件判断拼接（动态sql）
        // 3.1 创建条件构造器
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        // 3.2 分别判断条件是否为空
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified", end);
        }

        return baseMapper.selectPage(teacherPage, queryWrapper);
    }
}
